const { User } = require('../models/users');
const sha1  = require('sha1');
const moment = require('moment');
const objectIdToTimestamp = require('objectid-to-timestamp');
const sendMsg = require('../utils').SendMsg
// 获取用户唯一token值
exports.getUserToken = async (ctx, next) => {
    const username = ctx.request.body.username;
    const password = sha1(ctx.request.body.password);
    const telephone = ctx.request.body.telephone;

    if (username && !telephone) { // 帐号登录
       let data = await User.findOne({
           username: username
       }, 'password');
        if (data) {
            if (data.password === password) {
                let result = await User.findOne({
                    username: username,
                    password: password
                }, '_id')
                ctx.body = {
                    status: 0,
                    result: {
                        token: result._id
                    }
                }
            } else {
                ctx.body = sendMsg(-20004)
            }
        } else {
            ctx.body = sendMsg(-20007)
        }
    } else if (telephone && !username || telephone && username) {  // 手机号登录
        let data = await User.findOne({
            telephone: telephone
        }, 'password')
        if (data) {
            if (data.password === password) {
                 let result = await User.findOne({
                    telephone: telephone,
                    password: password
                }, '_id')
                ctx.body = {
                    status: 0,
                    result: {
                        token: result._id
                    }
                }
            } else {
                ctx.body = sendMsg(-20004)
            }
        } else {
            ctx.body = sendMsg(-20008)
        }
    } else {
        ctx.body = sendMsg(-20005)
    }
}

// 根据token获取用户所有信息
exports.getUserInfoByToken = async (ctx, next) => {
    const token = ctx.request.body.token;
    let data = await User.findById(token)
    ctx.body = {
        status: 0,
        result: data
    }
}

// 增加一个新用户
exports.addUser = async (ctx, next) => {
    let addUserInfo = {
        username: ctx.request.body.username || null,
        password: sha1(ctx.request.body.password),
        telephone: ctx.request.body.telephone,
        avatar: ctx.request.body.avatar || null,
        abstract: ctx.request.body.abstract || null,
        apartment: ctx.request.body.apartment || null,
        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        roles:  ctx.request.body.roles || [0]
    }
    console.log(addUserInfo)
    let checkUser = await User.findOne({
        telephone: addUserInfo.telephone
    })
    if (!checkUser) {
        var data = await User.create(addUserInfo);
        if (!data) {

        } else {
            ctx.body = {
                status: 0,
                result: {
                    msg: '注册成功'
                }
            }
        }
    } else {
        ctx.body = sendMsg(-20002)
    }
}

// 删除一个用户
exports.removeUser = async (ctx, next) => {
    const token = ctx.request.body.token;
    let check = await User.findOne({
        _id: token
    })
    if (!check) {
        ctx.body = sendMsg(-20009)
    } else {
        let data = await User.remove({_id: token})
        ctx.body = {
            status: 0,
            result: data
        }
    }
}

// 修改一个用户
exports.editUser = async (ctx, next) => {
    const token  = ctx.request.body.token;
    let UserInfo = {
        username: ctx.request.body.username,
        telephone: ctx.request.body.telephone,
        abstract: ctx.request.body.abstract,
        apartment: ctx.request.body.apartment
    }
    let check = await User.findOne({
        _id: token
    })
    if (!check) {
        ctx.body = sendMsg(-20009)
    } else {
        let data = await User.update({ _id: token}, {$set: {username: UserInfo.username, telephone:　UserInfo.telephone, abstract: UserInfo.abstract, apartment: UserInfo.apartment}})
        if (!data) {
            ctx.body = sendMsg(-20011)
        } else {
             ctx.body = {
                status: 0,
                result: {
                    msg: '修改成功'
                }
            }
        }
    }
}



